<?php

class WeishareAction extends CommonAction
{
    public function index()
    {
        $shareDB = D('weixinshare');
        $map='s.site_id = '.$this->siteid;
        $count = $shareDB->alias('s')->where($map)->count();


        $pagesize = 10;
        if ($this->_get('pagesize')) {
            $pagesize = $this->_get('pagesize');
        }

        if ($this->_get('keywords')){
            if(ctype_digit($this->_get('keywords'))){
                $map .=" and a.id like '%".$this->_get('keywords')."%'";
            }else{
                $map .= " and a.nickname like '%" . $this->_get('keywords') . "%'";
            }
            $count=$shareDB->alias('s')->join('left join think_member as a on a.id = s.member_id')->where($map)->count();
            $this->assign('keywords',$this->_get('keywords'));
        }
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, $pagesize);

        $data = $shareDB->alias('s')->join('left join think_member as a on a.id = s.member_id')->where($map)->order('datetime DESC')->limit($p->firstRow . ',' . $p->listRows)  -> select();

        $pages = $p->show();
        $this->assign('list', $data);
        $this->assign('pages', $pages);
        $this->display();
    }

    public function belong(){
        $shareDB = D('weixinshare_log');
        $map='s.site_id = '.$this->siteid;
        $count = $shareDB->alias('s')->where($map)->count();

        $pagesize = 10;
        if ($this->_get('pagesize')) {
            $pagesize = $this->_get('pagesize');
        }
        //实例化分页类
        import('ORG.Util.Page');
        if ($this->_get('keywords')){
            if(ctype_digit($this->_get('keywords'))){//搜索id
                $map .=" and s.member_id like '%".$this->_get('keywords')."%' or s.aff_id like '%".$this->_get("keywords")."%' and s.site_id = ".$this->siteid;//or优先级倒数第一，and优先级倒数第二，and比or优先级高
                $count = $shareDB->alias('s')->where($map)->count();
                $p = new Page($count, $pagesize);
                $data = $shareDB->alias('s')->where($map)->order('ctime DESC')->limit($p->firstRow . ',' . $p->listRows)  -> select();
                foreach ($data as $key => $value) {
                    $data[$key]['nickname'] = D('member')-> where('id = '.$value['member_id'])  ->select();
                    $data[$key]['belong']   = D('member')-> where('id = '.$value['aff_id'])     ->select();
                }
            }else{//搜索会员名 
                $map .= " and a.nickname like '%" . $this->_get('keywords') . "%'";//昵称
                $count1=$shareDB->alias('s')->join('left join think_member as a on a.id = s.member_id')->where($map)->count();
                $count2=$shareDB->alias('s')->join('left join think_member as a on a.id = s.aff_id')->where($map)->count();
                $count=$count1+$count2;
                $p = new Page($count, $pagesize);
                $data = $shareDB->alias('s')->join('left join think_member as a on a.id = s.aff_id or a.id = s.member_id')->where($map)->order('ctime DESC')->limit($p->firstRow . ',' . $p->listRows)  -> select();
                foreach ($data as $key => $value) {
                    $data[$key]['nickname'] = D('member')-> where('id = '.$value['member_id'])  ->select();
                    $data[$key]['belong']   = D('member')-> where('id = '.$value['aff_id'])     ->select();
                }
            }
            $this->assign('keywords',$this->_get('keywords'));
        }else{//无关键字模式
            $p = new Page($count, $pagesize);
            $data = $shareDB->alias('s')->where($map)->order('ctime DESC')->limit($p->firstRow . ',' . $p->listRows)  -> select();
            foreach ($data as $key => $value) {
                $data[$key]['nickname'] = D('member')-> where('id = '.$value['member_id'])  ->select();
                $data[$key]['belong']   = D('member')-> where('id = '.$value['aff_id'])     ->select();
            }
        }

        $pages = $p->show();
        $this->assign('list', $data);
        $this->assign('pages', $pages);
        $this->display();
    }
}
